﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using welding.Model;

namespace welding
{

    public partial class login : Form
    {

        string device_code = string.Empty;
        string stn_code = string.Empty;

        public int access_right = 0;
        public string user_name = string.Empty;
        public string user_id = string.Empty;
        public string device_name = "CC01";
        public int barcode_num = 0;
        public string barcode_names = string.Empty;
        public int station_num = 0;
        public bool bprint_barcode = false;
        //计划加工数量
        public int planQty = 0;
        //开班时间
        public DateTime ShiftStartTime = new DateTime();

        //本班次加工完成数量
        public int finishedQuantity = 0;
        //本班次加工合格数量
        public int okQuantity = 0;
        private int ar;

        public List<ProductionDataClass> productionDatas = new List<ProductionDataClass>();

        XMLHelper xmlHelper = new XMLHelper();


        public login()
        {
            InitializeComponent();
        }

        private void login_Load(object sender, EventArgs e)
        {
            lblMessage.Text = string.Empty;
            tbUserName.Focus();
            tbPwd.Text = "";
            tbUserName.Text = "";
            btnLogin.Focus();
            panelLeftUp.BackColor = Color.FromArgb(200, 175, 181, 191);
            panelRightBottom.BackColor = Color.FromArgb(200, 175, 181, 191);
            vmTcpLogin.LoadSettings();
            vmTcpLogin.Start();
            vmSpLogin.LoadSettings();
            vmSpLogin.Start();
            loginTimer.Start();
            string strPlanNum = xmlHelper.ReadXml(cvs.DP.SYSTEM_CONFIG_FILE, "PLAN_NUM");
            tbPlanNum.Text = strPlanNum;
            string strDeviceCode = xmlHelper.ReadXml(cvs.DP.SYSTEM_CONFIG_FILE, "DEVICE_CODE");
            device_name = strDeviceCode;
            //device_name = "CC01";
        }

        private void tbUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                tbPwd.Focus();
            }
        }

        private void tbPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                //btnLogin.Focus();
                btnLogin_Click(null, null);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            vmTcpLogin.Stop();
            this.DialogResult = DialogResult.Cancel;
            this.Close();

            //Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            vmTcpLogin.Clear();
            bool v = int.TryParse(tbPlanNum.Text, out planQty);
            if (v == false || planQty < 0 || planQty > 2000)
            {
                lblMessage.Text = "计划数量错误";
                return;
            }
            if (tbUserName.Text.Trim() == string.Empty)
            {
                lblMessage.Text = "用户名不能为空";
                return;
            }

            if (tbPwd.Text.Trim() == string.Empty)
            {
                lblMessage.Text = "密码不能为空";
                return;
            }
            //发送登录信息
            string strLogin = "LOGIN," + DateTime.Now.ToString("HHmmss") + "," + device_name + "," + tbUserName.Text.Trim() + "," + tbPwd.Text.Trim() + "," + tbPlanNum.Text;
            vmTcpLogin.Set(strLogin);
            bool bInTime = false;
            bool verifyTryParse;

            //vmListView1.Set("1" + "," + "2", Color.Black);
            for (int i = 0; i < 90; i++)
            {
                string strTcpIpGet = vmTcpLogin.Get();
                if (strTcpIpGet.Length > 0)
                {
                    string[] strs = strTcpIpGet.Split(',');
                    if (strs.Length < 5) continue;//回复格式错误
                    if (strs[0].Contains("LOGIN") == false) continue;//非登录信息回复
                    bInTime = true;
                    lblMessage.Text = "";

                    if (strs[3] == "OK")
                    {

                        xmlHelper.WriteXml(cvs.DP.SYSTEM_CONFIG_FILE, "PLAN_NUM", tbPlanNum.Text);

                        access_right = Convert.ToInt16(strs[4]);
                        user_name = strs[5];
                        user_id = tbUserName.Text.Trim();
                        //格式化加工数据
                        if (strs[6] != string.Empty)
                        {
                            productionDatas.Clear();

                            string[] _data = strs[6].Split(';');
                            foreach (string _str in _data)
                            {
                                ProductionDataClass _productionData = new ProductionDataClass();
                                if ((_str != null) && (_str != string.Empty))
                                {
                                    //config, finishedQty, okQty, ngQty, repairedQty, reportYield
                                    string[] _item = _str.Split(':');
                                    if (_item.Length >= 6)
                                    {
                                        _productionData.config = _item[0];

                                        bool _v = int.TryParse(_item[1], out _productionData.finishedQty);
                                        _v = int.TryParse(_item[2], out _productionData.okQty);
                                        _v = int.TryParse(_item[3], out _productionData.ngQty);
                                        _v = int.TryParse(_item[4], out _productionData.repairedQty);
                                        _v = int.TryParse(_item[5], out _productionData.reportYield);
                                        productionDatas.Add(_productionData);
                                    }
                                }
                            }
                        }
                        //verifyTryParse = int.TryParse(strs[5],out finishedQuantity);
                        //verifyTryParse = int.TryParse(strs[6], out okQuantity);
                        ShiftStartTime = Convert.ToDateTime(strs[7]);
                        vmTcpLogin.Stop();
                        vmSpLogin.Stop();
                        loginTimer.Stop();
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                        return;
                    }
                    else
                    {
                        lblMessage.Text = strs[5];
                        return;
                    }

                }
                else
                {
                    Thread.Sleep(100);
                }
            }
            if (!bInTime)
            {
                lblMessage.Text = "登录请求超时";
            }
        }

        private void login_Shown(object sender, EventArgs e)
        {
            //btnLogin.Focus();
            tbUserName.Focus();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            vmTcpLogin.SaveSettings();
            vmSpLogin.SaveSettings();
        }
        private void loginTimer_Tick(object sender, EventArgs e)
        {
            string strScanner = vmSpLogin.Get();

            if (strScanner == "") return;

            if (tbUserName.Text == "")
            {
                tbUserName.Text = strScanner;
                return;
            }

            if (tbPwd.Text == "")
            {
                tbPwd.Text = strScanner;
                btnLogin_Click(null, null);
                return;
            }

        }

        private void btnResetLogin_Click(object sender, EventArgs e)
        {
            tbUserName.Text = "";
            tbPwd.Text = "";
        }
    }
    public class ProductionData
    {
        public string deviceId;
        public string config;
        public int planQty;
        public int finishedQty;
        public int okQty;
        public int ngQty;
        public int repairedQty;
        public int reportYield;         //自动报产的数量

        public ProductionData()
        {
            this.deviceId = string.Empty;
            config = string.Empty;
            planQty = 0;
            finishedQty = 0;
            okQty = 0;
            ngQty = 0;
            repairedQty = 0;
            reportYield = 0;
        }
    }
}
